package com.ecube.test;

import com.alibaba.fastjson.JSON;
import com.ecube.mqtt.MqttUtils;
import com.ecube.protocol.ProtocolHelper;
import com.ecube.protocol.common.DataReportHead;
import com.ecube.protocol.common.DataReportStandard;
import com.ecube.protocol.enums.PvFunctionEnum;
import com.ecube.protocol.pv.PvInverterInfo;
import com.ecube.protocol.pv.PvInverterState;
import com.ecube.utils.EnumUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 光伏
 *
 * @author zhuweijun
 * @version 1.0.0
 * @since 2025/7/31 19:19
 */
public class SendPvInverterInfo {

    public static void main(String[] args) throws Exception {

        // 光伏属性
        sendPvInfo("FAT", "100080001", "12025804");
        sendPvInfo("FAT", "100080001", "12025805");

        // 光伏状态
        while (true){
            sendPvState("FAT", "100080001", "12025804");
            sendPvState("FAT", "100080001", "12025805");
            Thread.sleep(1000*10);
        }
    }

    private static void sendPvState(String env, String productCode, String deviceCode) {
        PvInverterState inverterState = new PvInverterState();
        PvFunctionEnum function = (PvFunctionEnum) EnumUtils.getFunction(inverterState.getClass());
        if(function == null){
            return;
        }

        String pubTopic = productCode + "/" + deviceCode + "/" + function.getDirection() + "/" + function.getType();

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        DataReportStandard reportStandard = new DataReportStandard();
        reportStandard.setHeader(ProtocolHelper.getReportHand(productCode, deviceCode, function.getFunction()));

        reportStandard.setDataBody(inverterState);

        String reportContent = JSON.toJSONString(reportStandard);
        System.out.println("发送报文内容：" + reportContent);
        MqttUtils.sendMQTT(env, pubTopic, reportContent);
        String time = dateFormat.format(date);
        MqttUtils.sendMQTT(env, productCode + "/" + deviceCode + "/S2M/keepalive", time);
    }

    private static void sendPvInfo(String env, String productCode, String deviceCode) {
        PvInverterInfo inverterInfo = new PvInverterInfo();
        PvFunctionEnum function = (PvFunctionEnum) EnumUtils.getFunction(inverterInfo.getClass());
        if(function == null){
            return;
        }

        String pubTopic = productCode + "/" + deviceCode + "/" + function.getDirection() + "/" + function.getType();
        DataReportStandard reportStandard = new DataReportStandard();
        DataReportHead reportHand = ProtocolHelper.getReportHand(productCode, deviceCode, function.getFunction());
        reportStandard.setHeader(reportHand);
        reportStandard.setDataBody(inverterInfo);

        String reportContent = JSON.toJSONString(reportStandard);
        System.out.println("发送报文内容：" + reportContent);
        MqttUtils.sendMQTT(env, pubTopic, reportContent);

        // 心跳空报文
        reportHand.setFunction("keepalive");
        DataReportStandard reportStandard1 = new DataReportStandard();
        reportStandard1.setHeader(reportHand);
        reportStandard1.setDataBody(new Object());
        MqttUtils.sendMQTT(env, pubTopic, JSON.toJSONString(reportStandard1));
    }
}
